<template>
  <div>
    <router-link to="/admin/add">
      <el-button type="danger">添加</el-button>
    </router-link>
    <!-- 全部打开：default-expand-all -->
    <el-table
      :data="tableData"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column prop="uid" label="用户ID" width="300px"></el-table-column>
      <el-table-column prop="username" label="用户名"></el-table-column>
      <el-table-column prop="rolename" label="角色名称"></el-table-column>
      <el-table-column prop="status" label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status==1" type="success" size="medium">已启用</el-tag>
          <el-tag v-else type="danger" size="medium">已禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            circle
            size="mini"
            @click="edit(scope.row.uid)"
          ></el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            circle
            size="mini"
            @click="del(scope.row.uid)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page.sync="currentPage"
      :page-size="pageSize"
      layout="prev, pager, next, jumper,total"
      :total="total"
    ></el-pagination>
  </div>
</template>
<script>
import { getAdminNum ,getUserList} from "../../request";
export default {
  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 2,
      total: 0
    };
  },
  created() {
    // 获取管理员总个数
    this.getAdminNumber();
  },
  methods: {
    // 获取总条数
    async getAdminNumber() {
      let res = await getAdminNum();
      this.total = res[0].total;
      this.getData();
    },
    async getData() {
        let res=await getUserList({
          size:this.pageSize,
          page:this.currentPage
        })
        this.tableData = res
    },
    // 当前页
    handleCurrentChange(val) {
      this.getData();
    },
    edit(uid) {
      this.$router.push("/admin/" + uid);
    },
    del(uid) {
      this.$confirm("确认删除吗,是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type:"warning",
        confirmButtonClass:"el-button--danger"
      })
        .then(res => {
          this.doDel(uid);
          this.$router.go(0);
        })
        .catch(_ => {});
    },
    doDel(uid) {
      this.$http.post('/userdelete', {uid}).then(res => {
        this.tableData = res.data.list;
      });
    }
  }
};
</script>
<style lang="" >
.el-button {
  margin-bottom: 10px;
}
.el-pagination__total{
  float: right;
}
</style>